package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;
import java.util.function.Function;

@Tag(name = "专辑管理")
@RestController
@RequestMapping("api/album/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class AlbumInfoApiController {

	@Autowired
	private AlbumInfoService albumInfoService;

	@Operation(summary = "获取当前用户专辑列表")
	@GetMapping("/findUserAllAlbumList")
	public Result findUserAllAlbumList(){
		Long userId = AuthContextHolder.getUserId()==null?1L:AuthContextHolder.getUserId();
		List<AlbumInfo> list=albumInfoService.findUserAllAlbumList(userId);
		return Result.ok(list);
	}

	@Operation(summary = "修改专辑信息")
	@PutMapping("updateAlbumInfo/{id}")
	public Result updateAlbumInfo(@PathVariable Long id, @RequestBody AlbumInfoVo albumInfoVo){

		albumInfoService.updateAlbumInfo(id,albumInfoVo);
		return Result.ok();
	}

	@Operation(summary = "根据专辑id回显数据")
	@GetMapping("/getAlbumInfo/{id}")
	public Result getAlbumInfo(@PathVariable Long id){
		AlbumInfo albumInfo = albumInfoService.getAlbumInfo(id);
		return Result.ok(albumInfo);
	}

	@Operation(summary = "根据专辑id删除专辑")
	@DeleteMapping("/removeAlbumInfo/{id}")
	public Result removeAlbumInfo(@PathVariable Long id){
		albumInfoService.removeAlbumInfo(id);
		return Result.ok();
	}

	@Operation(summary = "提交专辑")
	@PostMapping("/saveAlbumInfo")
	public Result saveAlbumInfo(@RequestBody AlbumInfoVo albumInfoVo){

		// 获取用户id，还没完成
		Long userId = AuthContextHolder.getUserId()==null?1L:AuthContextHolder.getUserId();

		albumInfoService.saveAlbumInfo(albumInfoVo,userId);
		return Result.ok();
	}

	@Operation(summary = "查看专辑列表")
	@PostMapping("/findUserAlbumPage/{page}/{limit}")
	public Result findUserAlbumPage(@PathVariable Long page,
	                                @PathVariable Long limit,
									@RequestBody AlbumInfoQuery albumInfoQuery){
		// 获取用户id，还没完成
		Long userId = AuthContextHolder.getUserId()==null?1L:AuthContextHolder.getUserId();
		albumInfoQuery.setUserId(userId);
		Page<AlbumListVo> albumListVoPage=new Page<>(page,limit);
		//	调用服务层方法
		IPage<AlbumListVo> iPage = albumInfoService.findUserAlbumPage(albumListVoPage,albumInfoQuery);
		return Result.ok(iPage);
	}


}

